package io.renren.modules.diycake.service.impl;

import io.renren.modules.diycake.entity.UnitEntity;
import io.renren.modules.diycake.service.UnitService;
import io.renren.modules.diycake.vo.UnitDepUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.diycake.dao.DepartmentDao;
import io.renren.modules.diycake.entity.DepartmentEntity;
import io.renren.modules.diycake.service.DepartmentService;


@Service("departmentService")
public class DepartmentServiceImpl extends ServiceImpl<DepartmentDao, DepartmentEntity> implements DepartmentService {

    @Autowired
    private UnitService unitService;
    @Autowired
    private DepartmentService departmentService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DepartmentEntity> page = this.page(
                new Query<DepartmentEntity>().getPage(params),
                new QueryWrapper<DepartmentEntity>()
        );
        List<DepartmentEntity> collect = page.getRecords().stream().map(item -> {
            UnitEntity unitEntity = unitService.getById(item.getUnitId());
            item.setUnitName(unitEntity.getName());
            return item;
        }).collect(Collectors.toList());

        page.setRecords(collect);

        return new PageUtils(page);
    }

    @Override
    public List<UnitDepUserVo> getVo() {
        List<UnitEntity> unitEntities = unitService.list(null);
        List<DepartmentEntity> departmentEntities = departmentService.list();
        List<UnitDepUserVo> collect = unitEntities.stream().map(unitEntity -> {
            UnitDepUserVo unitDepUserVo = new UnitDepUserVo();
            unitDepUserVo.setLabel(unitEntity.getName());
            unitDepUserVo.setValue(unitEntity.getId());
            unitDepUserVo.setChildren(getChildren(unitEntity, departmentEntities));
            return unitDepUserVo;
        }).collect(Collectors.toList());
        return collect;

    }

    public List<UnitDepUserVo> getChildren(UnitEntity unitEntity,List<DepartmentEntity> departmentEntities){
        List<UnitDepUserVo> collect = departmentEntities.stream()
                .filter(departmentEntity -> departmentEntity.getUnitId() == unitEntity.getId())
                .map(departmentEntity -> {
                    UnitDepUserVo unitDepUserVo = new UnitDepUserVo();
                    unitDepUserVo.setValue(departmentEntity.getId());
                    unitDepUserVo.setLabel(departmentEntity.getDepartmentName());
                    return unitDepUserVo;
                }).collect(Collectors.toList());
        return collect;
    }
}